define(["jquery","bbcare","fu/app/comm/comm"],function ($,$bb,$comm) {
    var servers = {};
    var URL = $bb.baseUrl("followup");

    /**
     * 请求历史记录列表
     */
    servers.ajaxHistory = function(params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getHistoryScore",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求历史记录列表出错！");
                }
            }
        });
    };

    /**
     * 获取胎龄
     */
    var getDiff = function(birthday,date,pregnancDay){
        var pregnancDay = parseInt(pregnancDay);
        var days;
        if( pregnancDay < 40*7 ){               // 小于40周：
            if( birthday ){                     // 出生：   days = 孕周 + 出生周龄
                days = $comm.dateDiff(birthday,date).days + pregnancDay;
            }else{                              // 未出生： days = 孕周;
                days = pregnancDay;
            }
        }else{                                  // 大于40周；
            if( birthday ){                     // 出生：   days = 出生周龄 + 40*7;
                days = $comm.dateDiff(birthday,date).days + 40*7;
            }else{                              // 未出生： days = ;  //不存在此情况：禁用：
                days = '';
            }
        }
        return days;
    };

    /**
     * 根据item获取stage
     */
    var getStageByItem = function(item){
        var arr =  [
            {'item':'N',"status":1,"itemColor":"#AACA57","stage":"扭动阶段正常GMs"},
            {'item':'PR',"status":1,"itemColor":"#FACE64","stage":"单调性GMs"},
            {'item':'CS',"status":1,"itemColor":"#EF7255","stage":"痉挛-同步性GMs"},
            {'item':'Ch',"status":1,"itemColor":"#ED9772","stage":"混乱性GMs"},
            {'item':'F',"status":1,"itemColor":"#70C6C9","stage":"不安运动阶段的正常GMs"},
            {'item':'F-',"status":1,"itemColor":"#3B6FB3","stage":"不安运动缺乏"},
            {'item':'AF',"status":1,"itemColor":"#8B549B","stage":"异常性不安运动"},
        ];
        for (var i = 0; i < arr.length; i++) {
            if( arr[i].item == item ){
                return arr[i].stage;
            }
        }
    }

    /**
     * 处理基本信息
     */
    servers.handleInfo = function(info){
        var data = $.extend({},info);
        for(var key in data){
            if( data[key] ){
                if( key == "birthday" ){
                    var nowDate = $comm.getCurTime().date;
                    var days = $comm.dateDiff(data.birthday,nowDate).days;
                    data.weekAge = parseInt(days / 7) + "周" + days % 7 + "天";
                }
                if( key == "pregnancyWeek"){
                    if( typeof data[key] == "number") data.pregnancDay = data[key];
                    data[key] = parseInt(data.pregnancDay / 7) + "周" + data.pregnancDay % 7 + "天";
                }
                if( key == "gender" ) data[key] = data[key] == "F" ? "女" : "男";
                if( key == "answerResult" ){
                    if( data[key] && typeof data[key] == "string" ) data[key] = JSON.parse(data[key]);
                    data.result = $.extend({},data[key].ps);
                    var embryonicDays = getDiff(data.birthday,data.testTime,data.pregnancDay);
                    data.result.embryonicAge = parseInt(embryonicDays / 7) + "周" + embryonicDays % 7 + "天";
                    if( !data.result.stage ) data.result.stage = getStageByItem(data.result.item);
                }
            }else{
                data[key] = "未知";
                if( key == "birthday" ) data.weekAge = "未知";
            }
        }
        return data;
    }

    /**
     * 获取曲线图上需要的数据
     */
    servers.getGmsScore = function (params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getGmsScore",
            data: JSON.stringify({
                "data": {
                    "examinationPaperName": params.type,
                    "userId": params.userId,
                    "evaluationId": params.answerId,
                    "testTime": params.testTime
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,params);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求数据出错！");
                }
            }
        });
    };

    /**
     * 将结果转换为Y轴坐标
     */
    var getYval = function(res){
        var evaY;
        if(res.item == "F"){
            evaY = 2;
        }else if(res.item == "N"){
            evaY = 1;
        }else if(res.item == "PR"){
            evaY = 3;
        }else if(res.item == "Ch"){
            evaY = 4;
        }else if(res.item == "CS"){
            evaY = 5;
        }else if(res.item == "AF"){
            evaY = 6;
        }else if(res.item == "F-"){
            evaY = 7;
        }
        return evaY;
    }

    /**
     * 处理图表里需要用的数据
     */
    servers.curDeal = function(data){
        var options = data.option;
        var result = data.result;
        var history = [];
        for( var  i = 0; i < result.length; i++ ){
            var res = JSON.parse( result[i].result );
            res.pregnancDay = result[i].pregnancyWeek;
            res.testTime = result[i].testTime;
            res.evaRes = options[res.item];
            res.itemNum = getYval(res);
            history.push({
                name:JSON.stringify(res),
                value: [res.days, res.itemNum],
                itemStyle: {
                    normal: {
                        color: res.color
                    },
                    emphasis: {
                        color: res.color
                    }
                }
            });
        }
        return history;
    }

    //虚拟数据：
    servers.diotArr = function(){
        var arr = [];
        var min = 185.5,y1 = 6.48,max = 315;
        var num  = 7,i=0,j= 0,k= 1;
        do{
            var y = num*i;
            i++;
            arr.push([(min+y),y1]);
        }while((min+y)<max);

        var min2 = 339.5,y2= 3.48,max2 = 430;
        do{
            var y = num*j;
            j++;
            arr.push([(min2+y),y2]);
        }while((min2+y)<max2);

        var c= 0.5;
        do{
            var y = c*k;
            k++;
            arr.push([325.5,y]);
            arr.push([332.5,y]);
        }while((c+y)<7);
        return arr;
    }

    /*
    * 获取指导意见、备注
    * */
    servers.getGuides = function (params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getGuide",
            data: JSON.stringify({
                "data": {
                    "examinationPaperName": params.type,
                    "userId": params.userId,
                    "evaluationId": params.answerId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,params);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "获取指导意见、备注出错！");
                }
            }
        });
    };

    /*
     * 保存建议和备注
     * */
    servers.saveGuides = function (params,callback,info){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/guidemodify",
            data: JSON.stringify({
                "data": params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,info);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "保存指导意见、备注出错！");
                }
            }
        });
    };
     
    return servers;
});